<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Importing and Exporting Data to and from cpp_int and cpp_bin_float</title>
<link rel="stylesheet" href="../../multiprecision.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../index.html" title="Chapter 1. Boost.Multiprecision">
<link rel="up" href="../tut.html" title="Tutorial">
<link rel="prev" href="lits.html" title="Literal Types and constexpr Support">
<link rel="next" href="rounding.html" title="Rounding Rules for Conversions">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="lits.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tut.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="rounding.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_multiprecision.tut.import_export"></a><a class="link" href="import_export.html" title="Importing and Exporting Data to and from cpp_int and cpp_bin_float">Importing and
      Exporting Data to and from <code class="computeroutput"><span class="identifier">cpp_int</span></code>
      and <code class="computeroutput"><span class="identifier">cpp_bin_float</span></code></a>
</h3></div></div></div>
<p>
        Any integer number type that uses <code class="computeroutput"><span class="identifier">cpp_int_backend</span></code>
        as its implementation layer can import or export its bits via two non-member
        functions:
      </p>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">unsigned</span> <span class="identifier">MinBits</span><span class="special">,</span> <span class="keyword">unsigned</span> <span class="identifier">MaxBits</span><span class="special">,</span> <span class="identifier">cpp_integer_type</span> <span class="identifier">SignType</span><span class="special">,</span> <span class="identifier">cpp_int_check_type</span> <span class="identifier">Checked</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Allocator</span><span class="special">,</span>
          <span class="identifier">expression_template_option</span> <span class="identifier">ExpressionTemplates</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">OutputIterator</span><span class="special">&gt;</span>
<span class="identifier">OutputIterator</span> <span class="identifier">export_bits</span><span class="special">(</span>
   <span class="keyword">const</span> <span class="identifier">number</span><span class="special">&lt;</span><span class="keyword">const</span> <span class="identifier">cpp_int_backend</span><span class="special">&lt;</span><span class="identifier">MinBits</span><span class="special">,</span> <span class="identifier">MaxBits</span><span class="special">,</span> <span class="identifier">SignType</span><span class="special">,</span> <span class="identifier">Checked</span><span class="special">,</span> <span class="identifier">Allocator</span><span class="special">&gt;,</span> <span class="identifier">ExpressionTemplates</span><span class="special">&gt;&amp;</span> <span class="identifier">val</span><span class="special">,</span>
   <span class="identifier">OutputIterator</span> <span class="identifier">out</span><span class="special">,</span>
   <span class="keyword">unsigned</span> <span class="identifier">chunk_size</span><span class="special">,</span>
   <span class="keyword">bool</span> <span class="identifier">msv_first</span> <span class="special">=</span> <span class="keyword">true</span><span class="special">);</span>

<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">unsigned</span> <span class="identifier">MinBits</span><span class="special">,</span> <span class="keyword">unsigned</span> <span class="identifier">MaxBits</span><span class="special">,</span> <span class="identifier">cpp_integer_type</span> <span class="identifier">SignType</span><span class="special">,</span> <span class="identifier">cpp_int_check_type</span> <span class="identifier">Checked</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Allocator</span><span class="special">,</span>
          <span class="identifier">expression_template_option</span> <span class="identifier">ExpressionTemplates</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Iterator</span><span class="special">&gt;</span>
<span class="identifier">number</span><span class="special">&lt;</span><span class="identifier">cpp_int_backend</span><span class="special">&lt;</span><span class="identifier">MinBits</span><span class="special">,</span> <span class="identifier">MaxBits</span><span class="special">,</span> <span class="identifier">SignType</span><span class="special">,</span> <span class="identifier">Checked</span><span class="special">,</span> <span class="identifier">Allocator</span><span class="special">&gt;,</span> <span class="identifier">ExpressionTemplates</span><span class="special">&gt;&amp;</span>
   <span class="identifier">import_bits</span><span class="special">(</span>
      <span class="identifier">number</span><span class="special">&lt;</span><span class="identifier">cpp_int_backend</span><span class="special">&lt;</span><span class="identifier">MinBits</span><span class="special">,</span> <span class="identifier">MaxBits</span><span class="special">,</span> <span class="identifier">SignType</span><span class="special">,</span> <span class="identifier">Checked</span><span class="special">,</span> <span class="identifier">Allocator</span><span class="special">&gt;,</span> <span class="identifier">ExpressionTemplates</span><span class="special">&gt;&amp;</span> <span class="identifier">val</span><span class="special">,</span>
      <span class="identifier">Iterator</span> <span class="identifier">i</span><span class="special">,</span>
      <span class="identifier">Iterator</span> <span class="identifier">j</span><span class="special">,</span>
      <span class="keyword">unsigned</span> <span class="identifier">chunk_size</span> <span class="special">=</span> <span class="number">0</span><span class="special">,</span>
      <span class="keyword">bool</span> <span class="identifier">msv_first</span> <span class="special">=</span> <span class="keyword">true</span><span class="special">);</span>
</pre>
<p>
        These functions are designed for data-interchange with other storage formats,
        and since <a class="link" href="floats/cpp_bin_float.html" title="cpp_bin_float">cpp_bin_float</a>
        uses <a class="link" href="ints/cpp_int.html" title="cpp_int">cpp_int</a>
        internally, by extension they can be used for floating-point numbers based
        on that backend as well (see example below). Parameters and use are as follows:
      </p>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">unsigned</span> <span class="identifier">MinBits</span><span class="special">,</span> <span class="keyword">unsigned</span> <span class="identifier">MaxBits</span><span class="special">,</span> <span class="identifier">cpp_integer_type</span> <span class="identifier">SignType</span><span class="special">,</span> <span class="identifier">cpp_int_check_type</span> <span class="identifier">Checked</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Allocator</span><span class="special">,</span>
          <span class="identifier">expression_template_option</span> <span class="identifier">ExpressionTemplates</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">OutputIterator</span><span class="special">&gt;</span>
<span class="identifier">OutputIterator</span> <span class="identifier">export_bits</span><span class="special">(</span>
   <span class="keyword">const</span> <span class="identifier">number</span><span class="special">&lt;</span><span class="keyword">const</span> <span class="identifier">cpp_int_backend</span><span class="special">&lt;</span><span class="identifier">MinBits</span><span class="special">,</span> <span class="identifier">MaxBits</span><span class="special">,</span> <span class="identifier">SignType</span><span class="special">,</span> <span class="identifier">Checked</span><span class="special">,</span> <span class="identifier">Allocator</span><span class="special">&gt;,</span> <span class="identifier">ExpressionTemplates</span><span class="special">&gt;&amp;</span> <span class="identifier">val</span><span class="special">,</span>
   <span class="identifier">OutputIterator</span> <span class="identifier">out</span><span class="special">,</span>
   <span class="keyword">unsigned</span> <span class="identifier">chunk_size</span><span class="special">,</span>
   <span class="keyword">bool</span> <span class="identifier">msv_first</span> <span class="special">=</span> <span class="keyword">true</span><span class="special">);</span>
</pre>
<p>
        Exports the absolute value of <code class="computeroutput"><span class="identifier">val</span></code>
        to OutputIterator <code class="computeroutput"><span class="identifier">out</span></code>. The
        function will write <code class="computeroutput"><span class="identifier">chunk_size</span></code>
        bits at a time to the OutputIterator, and if <code class="computeroutput"><span class="identifier">msv_first</span></code>
        is true, will write the most-significant block first. Byte and bit order
        within each <code class="computeroutput"><span class="identifier">chunk_size</span></code> block
        is always in the machines native format. Further, each block is stored in
        a <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">uintmax_t</span></code> when it's assigned to <code class="computeroutput"><span class="special">*</span><span class="identifier">out</span></code>.
      </p>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../doc/src/images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
          Unfortunately, the standard's OutputIterator concept provides no means
          of deducing the type to output since <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">iterator_traits</span><span class="special">&lt;</span><span class="identifier">OutputIteratorType</span><span class="special">&gt;::</span><span class="identifier">value_type</span></code>
          is type <code class="computeroutput"><span class="keyword">void</span></code>. This is why
          the bit count for each block has to be specified manually. It may also
          result in compiler warnings about the value being narrowed.
        </p></td></tr>
</table></div>
<div class="tip"><table border="0" summary="Tip">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Tip]" src="../../../../../../doc/src/images/tip.png"></td>
<th align="left">Tip</th>
</tr>
<tr><td align="left" valign="top"><p>
          If you're exporting to non-native byte layout, then use <a href="http://www.boost.org/doc/libs/release/libs/endian/doc/index.html" target="_top">Boost.Endian</a>
          to create a custom OutputIterator that reverses the byte order of each
          chunk prior to actually storing the result.
        </p></td></tr>
</table></div>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">unsigned</span> <span class="identifier">MinBits</span><span class="special">,</span> <span class="keyword">unsigned</span> <span class="identifier">MaxBits</span><span class="special">,</span> <span class="identifier">cpp_integer_type</span> <span class="identifier">SignType</span><span class="special">,</span> <span class="identifier">cpp_int_check_type</span> <span class="identifier">Checked</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Allocator</span><span class="special">,</span>
          <span class="identifier">expression_template_option</span> <span class="identifier">ExpressionTemplates</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">ForwardIterator</span><span class="special">&gt;</span>
<span class="identifier">number</span><span class="special">&lt;</span><span class="identifier">cpp_int_backend</span><span class="special">&lt;</span><span class="identifier">MinBits</span><span class="special">,</span> <span class="identifier">MaxBits</span><span class="special">,</span> <span class="identifier">SignType</span><span class="special">,</span> <span class="identifier">Checked</span><span class="special">,</span> <span class="identifier">Allocator</span><span class="special">&gt;,</span> <span class="identifier">ExpressionTemplates</span><span class="special">&gt;&amp;</span>
   <span class="identifier">import_bits</span><span class="special">(</span>
      <span class="identifier">number</span><span class="special">&lt;</span><span class="identifier">cpp_int_backend</span><span class="special">&lt;</span><span class="identifier">MinBits</span><span class="special">,</span> <span class="identifier">MaxBits</span><span class="special">,</span> <span class="identifier">SignType</span><span class="special">,</span> <span class="identifier">Checked</span><span class="special">,</span> <span class="identifier">Allocator</span><span class="special">&gt;,</span> <span class="identifier">ExpressionTemplates</span><span class="special">&gt;&amp;</span> <span class="identifier">val</span><span class="special">,</span>
      <span class="identifier">ForwardIterator</span> <span class="identifier">i</span><span class="special">,</span>
      <span class="identifier">ForwardIterator</span> <span class="identifier">j</span><span class="special">,</span>
      <span class="keyword">unsigned</span> <span class="identifier">chunk_size</span> <span class="special">=</span> <span class="number">0</span><span class="special">,</span>
      <span class="keyword">bool</span> <span class="identifier">msv_first</span> <span class="special">=</span> <span class="keyword">true</span><span class="special">);</span>
</pre>
<p>
        Imports bits from the iterator range <span class="emphasis"><em>[i,j)</em></span> and stores
        them in <code class="computeroutput"><span class="identifier">val</span></code> to produce an
        unsigned result (if the result is to be signed you will need to handle that
        separately). When <code class="computeroutput"><span class="identifier">msv_first</span></code>
        is true, takes <code class="computeroutput"><span class="special">*</span><span class="identifier">i</span></code>
        as the most significant chunk. Assumes there are <code class="computeroutput"><span class="identifier">chunk_size</span></code>
        bits in each value read from the iterator range, and that these are in machine
        native bit/byte order. When <code class="computeroutput"><span class="identifier">chunk_size</span></code>
        is zero, then assumes that each chunk contains <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">iterator_traits</span><span class="special">&lt;</span><span class="identifier">ForwardIterator</span><span class="special">&gt;::</span><span class="identifier">value_type</span><span class="special">&gt;::</span><span class="identifier">digits</span></code>,
        note that this will give the wrong result if dereferencing the iterators
        leads to a signed-integer type, <span class="bold"><strong>and</strong></span> the
        sign bit is significant (be particularly careful if you expect type <code class="computeroutput"><span class="keyword">char</span></code> to contain 8-bit values, as by default
        it will extract only 7-bits at a time if <code class="computeroutput"><span class="keyword">char</span></code>
        is signed). As with exporting, if the external data is to be in a non-native
        byte order (within each chunk), then you will need to create an iterator
        adaptor that presents it in native order (see <a href="http://www.boost.org/doc/libs/release/libs/endian/doc/index.html" target="_top">Boost.Endian</a>).
      </p>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../doc/src/images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
          Note that this function is optimized for the case where the data can be
          <code class="computeroutput"><span class="identifier">memcpy</span></code>ed from the source
          to the integer - in this case both iterators much be pointers, and everything
          must be little-endian.
        </p></td></tr>
</table></div>
<h5>
<a name="boost_multiprecision.tut.import_export.h0"></a>
        <span class="phrase"><a name="boost_multiprecision.tut.import_export.examples"></a></span><a class="link" href="import_export.html#boost_multiprecision.tut.import_export.examples">Examples</a>
      </h5>
<p>
        In this simple example, we'll import/export the bits of a cpp_int to a vector
        of 8-bit unsigned values:
      </p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">multiprecision</span><span class="special">/</span><span class="identifier">cpp_int</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iostream</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iomanip</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">vector</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iterator</span><span class="special">&gt;</span>


<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
<span class="special">{</span>
   <span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">::</span><span class="identifier">cpp_int</span><span class="special">;</span>
   <span class="comment">// Create a cpp_int with just a couple of bits set:</span>
   <span class="identifier">cpp_int</span> <span class="identifier">i</span><span class="special">;</span>
   <span class="identifier">bit_set</span><span class="special">(</span><span class="identifier">i</span><span class="special">,</span> <span class="number">5000</span><span class="special">);</span> <span class="comment">// set the 5000'th bit</span>
   <span class="identifier">bit_set</span><span class="special">(</span><span class="identifier">i</span><span class="special">,</span> <span class="number">200</span><span class="special">);</span>
   <span class="identifier">bit_set</span><span class="special">(</span><span class="identifier">i</span><span class="special">,</span> <span class="number">50</span><span class="special">);</span>
   <span class="comment">// export into 8-bit unsigned values, most significant bit first:</span>
   <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">unsigned</span> <span class="keyword">char</span><span class="special">&gt;</span> <span class="identifier">v</span><span class="special">;</span>
   <span class="identifier">export_bits</span><span class="special">(</span><span class="identifier">i</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">back_inserter</span><span class="special">(</span><span class="identifier">v</span><span class="special">),</span> <span class="number">8</span><span class="special">);</span>
   <span class="comment">// import back again, and check for equality:</span>
   <span class="identifier">cpp_int</span> <span class="identifier">j</span><span class="special">;</span>
   <span class="identifier">import_bits</span><span class="special">(</span><span class="identifier">j</span><span class="special">,</span> <span class="identifier">v</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">v</span><span class="special">.</span><span class="identifier">end</span><span class="special">());</span>
   <span class="identifier">BOOST_ASSERT</span><span class="special">(</span><span class="identifier">i</span> <span class="special">==</span> <span class="identifier">j</span><span class="special">);</span>
<span class="special">}</span>
</pre>
<p>
        Importing or exporting cpp_bin_float is similar, but we must proceed via
        an intermediate integer:
      </p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">multiprecision</span><span class="special">/</span><span class="identifier">cpp_bin_float</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iostream</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iomanip</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">vector</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iterator</span><span class="special">&gt;</span>


<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
<span class="special">{</span>
   <span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">::</span><span class="identifier">cpp_bin_float_100</span><span class="special">;</span>
   <span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">::</span><span class="identifier">cpp_int</span><span class="special">;</span>
   <span class="comment">// Create a cpp_bin_float to import/export:</span>
   <span class="identifier">cpp_bin_float_100</span> <span class="identifier">f</span><span class="special">(</span><span class="number">1</span><span class="special">);</span>
   <span class="identifier">f</span> <span class="special">/=</span> <span class="number">3</span><span class="special">;</span>
   <span class="comment">// export into 8-bit unsigned values, most significant bit first:</span>
   <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">unsigned</span> <span class="keyword">char</span><span class="special">&gt;</span> <span class="identifier">v</span><span class="special">;</span>
   <span class="identifier">export_bits</span><span class="special">(</span><span class="identifier">cpp_int</span><span class="special">(</span><span class="identifier">f</span><span class="special">.</span><span class="identifier">backend</span><span class="special">().</span><span class="identifier">bits</span><span class="special">()),</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">back_inserter</span><span class="special">(</span><span class="identifier">v</span><span class="special">),</span> <span class="number">8</span><span class="special">);</span>
   <span class="comment">// Grab the exponent as well:</span>
   <span class="keyword">int</span> <span class="identifier">e</span> <span class="special">=</span> <span class="identifier">f</span><span class="special">.</span><span class="identifier">backend</span><span class="special">().</span><span class="identifier">exponent</span><span class="special">();</span>
   <span class="comment">// Import back again, and check for equality, we have to proceed via</span>
   <span class="comment">// an intermediate integer:</span>
   <span class="identifier">cpp_int</span> <span class="identifier">i</span><span class="special">;</span>
   <span class="identifier">import_bits</span><span class="special">(</span><span class="identifier">i</span><span class="special">,</span> <span class="identifier">v</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">v</span><span class="special">.</span><span class="identifier">end</span><span class="special">());</span>
   <span class="identifier">cpp_bin_float_100</span> <span class="identifier">g</span><span class="special">(</span><span class="identifier">i</span><span class="special">);</span>
   <span class="identifier">g</span><span class="special">.</span><span class="identifier">backend</span><span class="special">().</span><span class="identifier">exponent</span><span class="special">()</span> <span class="special">=</span> <span class="identifier">e</span><span class="special">;</span>
   <span class="identifier">BOOST_ASSERT</span><span class="special">(</span><span class="identifier">f</span> <span class="special">==</span> <span class="identifier">g</span><span class="special">);</span>
<span class="special">}</span>
</pre>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2002-2020 John
      Maddock and Christopher Kormanyos<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="lits.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tut.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="rounding.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
